package com.permissions.dispatcher.sample;

import com.permissions.dispatcher.annotation.*;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;

@RuntimePermissions
public class TestPermissionAbility extends Ability {

    public static final String CAMERA_PERMISSION_NAME = "ohos.permission.CAMERA";
    public static final String LOCATION_PERMISSION_NAME = "ohos.permission.LOCATION";
    public static final String WRITE_MEDIA_PERMISSION_NAME = "ohos.permission.WRITE_MEDIA";
    public static final String READ_MEDIA_PERMISSION_NAME = "ohos.permission.READ_MEDIA";
    public static final int REQUEST_PERMISSION_CODE = 11;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        //打开注释可以测试slice中的权限申请
//        super.setMainRoute(TestPermissionAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_test_permission);

        Button buttonStart = (Button) findComponentById(ResourceTable.Id_btn_test_apply);
        buttonStart.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                com.permissions.dispatcher.sample.TestPermissionAbilityPermissionsDispatcher.toOpenPermissionWithPermissionCheck(TestPermissionAbility.this);
            }
        });

        Button buttonJudge = (Button) findComponentById(ResourceTable.Id_btn_test_judge);
        buttonJudge.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                judgePermission();
            }
        });

    }


    @NeedsPermission({CAMERA_PERMISSION_NAME, LOCATION_PERMISSION_NAME})
    public void toOpenPermission() {
        ToastUtil("权限已申请成功");
    }

    @OnPermissionDenied({CAMERA_PERMISSION_NAME, LOCATION_PERMISSION_NAME})
    public void toResetPermission() {
        ToastUtil("OnPermissionDenied权限被拒绝了");
    }

    @OnShowRationale({CAMERA_PERMISSION_NAME, LOCATION_PERMISSION_NAME})
    public void showDialog(PermissionRequest request) {
        ToastUtil("showDialog解释你需要你打开相应权限原因,需要写dialog弹框选择");
        //确定申请
        request.proceed();
        //取消
//        request.cancel();
    }

    @OnNeverAskAgain({CAMERA_PERMISSION_NAME, LOCATION_PERMISSION_NAME})
    public void toExplainPermission() {
        ToastUtil("OnNeverAskAgain解释这个权限为什么被拒绝");
    }

    /**
     * 是否相应权限
     */
    public void judgePermission() {
        if (verifySelfPermission(CAMERA_PERMISSION_NAME) != IBundleManager.PERMISSION_GRANTED) {
            ToastUtil("还没有获取权限");
        } else {
            ToastUtil("已经获取了权限");
        }
    }

    /**
     * 申请权限
     */
    public void handleApplyPermission() {
        requestPermissionsFromUser(new String[]{CAMERA_PERMISSION_NAME}, REQUEST_PERMISSION_CODE);
    }


    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        com.permissions.dispatcher.sample.TestPermissionAbilityPermissionsDispatcher.onRequestPermissionsFromUserResult(this,requestCode,grantResults);
    }


    public void ToastUtil(String i) {
        ToastDialog dialog = new ToastDialog(this);
        dialog.setContentText(i);
        dialog.setDuration(2000);
        dialog.show();
    }
}
